package com.neu.tlias.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.tlias.pojo.Dept;
import com.neu.tlias.pojo.Emp;
import com.neu.tlias.pojo.PageBean;
import com.neu.tlias.pojo.Result;
import com.neu.tlias.service.IEmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>

 */
@Api(tags = "员工管理接口")
@Slf4j
@RestController
@RequestMapping("/emps")
@RequiredArgsConstructor
public class EmpController {

    private final IEmpService empService;

    @ApiOperation("新增员工接口")
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        empService.save(emp);
        return Result.success();
    }


    @ApiOperation("根据复杂条件查询员工接口")
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询，参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        //分页构造器
        Page<Emp> pageInfo = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!= null,Emp::getName,name)
                .eq(gender != null,Emp::getGender,gender)
                .between(begin != null && end != null,Emp::getEntrydate,begin,end);

        Page<Emp> p = empService.page(pageInfo,queryWrapper);

        long total = p.getTotal();
        List<Emp> empList = p.getRecords();
        PageBean pageBean = new PageBean(total,empList);

        return Result.success(pageBean);
    }

    @ApiOperation("根据id查询员工接口")
    @GetMapping("{id}")
    public Result getById(@ApiParam("用户id") @PathVariable("id") Long id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @ApiOperation("根据id修改员工接口")
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工:{}",emp);
        empService.updateById(emp);
        return Result.success();
    }

    @ApiOperation("删除员工接口")
    @DeleteMapping("/{ids}")
    public Result delete(@ApiParam("用户id") @PathVariable("ids") List<Integer> ids){
        log.info("批量删除操作，ids:{}",ids);
        empService.removeBatchByIds(ids);
        return Result.success();
    }
}
